package com.mjt.dongbao.ums.service.impl;

import com.mjt.dongbao.common.base.enums.StateCodeEnum;
import com.mjt.dongbao.common.base.result.ResultWrapper;
import com.mjt.dongbao.common.util.JwtUtil.JwtUtil;
import com.mjt.dongbao.ums.entity.UmsMember;
import com.mjt.dongbao.ums.entity.dto.UmsMemberLoginParamDto;
import com.mjt.dongbao.ums.entity.dto.UmsMemberRegisterParamDTO;
import com.mjt.dongbao.ums.entity.response.UmsMemberLoginResponse;
import com.mjt.dongbao.ums.mapper.UmsMemberMapper;
import com.mjt.dongbao.ums.service.UmsMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author 马俊涛
 * @since 2021-12-27
 */
@Service
public class UmsMemberServiceImpl implements UmsMemberService {

    @Autowired
    UmsMemberMapper umsMemberMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     *
     * @Description: 用户注册功能实现
     * @Author mjt
     * @Date 2022/1/13
     */
    @Override
    public ResultWrapper register(UmsMemberRegisterParamDTO umsMemberRegisterParamDTO) {
        // 将 DTO 转换成 实体对象
        UmsMember umsMember = new UmsMember();
        BeanUtils.copyProperties(umsMemberRegisterParamDTO, umsMember);

        // 将密码加密，不能明文存放在数据库
        String encode = passwordEncoder.encode(umsMemberRegisterParamDTO.getPassword());
        umsMember.setPassword(encode);
        int insertResult = umsMemberMapper.insert(umsMember);

        if (insertResult == 1){
            return ResultWrapper.getSuccessResult().build();
        }else {
            return ResultWrapper.getFailResult().build();
        }
    }

    /**
     *
     * @Description: 用户登录功能实现
     * @Author mjt
     * @Date 2022/1/13
     */
    @Override
    public ResultWrapper login(UmsMemberLoginParamDto umsMemberLoginParamDto) {
        // 根据用户名查询数据库
        UmsMember umsMember = umsMemberMapper.selectByName(umsMemberLoginParamDto.getUsername());
        if (null != umsMember){
            // 获取数据库中密码的密文
            String passwordDb = umsMember.getPassword();

            if (!passwordEncoder.matches(umsMemberLoginParamDto.getPassword(), passwordDb)) {
                return ResultWrapper.getFailResult().code(StateCodeEnum.PASSWORD_ERROR.getCode()).msg(StateCodeEnum.PASSWORD_ERROR.getMsg()).build();
            }
        }else {
            return ResultWrapper.getFailResult()
                    .code(StateCodeEnum.USER_EMPTY.getCode())
                    .msg(StateCodeEnum.USER_EMPTY.getMsg())
                    .build();
        }

        // 根据 userId 生成 token
        String token = JwtUtil.createToken(umsMember.getId() + "");
        UmsMemberLoginResponse umsMemberLoginResponse = new UmsMemberLoginResponse();
        umsMemberLoginResponse.setToken(token);
        umsMemberLoginResponse.setUmsMember(umsMember);

        return ResultWrapper.getSuccessResult().data(umsMemberLoginResponse).build();
    }

    /**
     *
     * @Description: 可统一代表用户登录之后的操作，应该需要校验 token。为避免重复性代码，校验 token 功能抽离出来，基于注解实现
     * @Author mjt
     * @Date 2022/1/13
     */
    @Override
    public ResultWrapper edit(UmsMember umsMember) {
        int updateResult = umsMemberMapper.updateById(umsMember);

        if (updateResult == 1){
            return ResultWrapper.getSuccessResult().data(umsMember).build();
        }else {
            return ResultWrapper.getFailResult().build();
        }
    }
}
